let strategyId = null;
const pageSize = 10;
let totalPages = 1;
document.addEventListener("DOMContentLoaded", function() {
    currentPage = parseInt(document.getElementById('currentPage').textContent);
    totalPages = parseInt(document.getElementById('pageNum').value);
    attachEventListeners();
    fetchData('/home?currentPage=' + currentPage + '&pageSize=' + pageSize);
});
function refreshData(page) {
    fetchData('/home?currentPage=' + page + '&pageSize=' + pageSize, page);
}
function fetchData(url, page) {
    fetch(url, {
        method: 'GET',
        headers: {
            'Content-Type': 'text/html'
        }
    })
        .then(response => response.text())
        .then(html => {
            document.getElementById('dataContainer').innerHTML = html;
            currentPage = page;
            document.getElementById('currentPage').textContent = currentPage;
            attachEventListeners();
        })
        .catch(error => {
            console.error('Error:', error);
        });
}
function updatePage(id) {
    if (id !== null) {
        strategyId = id;
        fetchData('/selectByStrategyId?currentPage=1&pageSize=' + pageSize + '&id=' + id, 1);
    } else {
        refreshData(1);
    }
}
function attachEventListeners() {}
function firstPageBtn() {
    fetchData('/home?currentPage=' + 1 + '&pageSize=' + pageSize, 1);
}
function prevBtn() {
    if (currentPage > 1) {
        fetchData('/home?currentPage=' + (currentPage-1)+ '&pageSize=' + pageSize, currentPage - 1);
    }
}
function nextBtn() {
    if (currentPage < totalPages) {
        fetchData('/home?currentPage=' + (currentPage+1) + '&pageSize=' + pageSize, currentPage + 1);
    }
}
function lastPageBtn() {
    if (currentPage !== totalPages) {
        fetchData('/home?currentPage=' + totalPages + '&pageSize=' + pageSize, totalPages);
    }
}
function search() {
    var searchText = document.getElementById("searchInput").value;
    var encodedSearchText = encodeURIComponent(searchText);
    var currentPage = 1;
    fetchData('/zelda/search?currentPage=' + currentPage + '&pageSize=' + pageSize + '&value=' + encodedSearchText, currentPage);
}